import cgi
import getopt
import locale
import os
import re
import shutil
import sys
import threading
import time
import traceback
import xml.etree.cElementTree as ET

from Util import *
from Doc4 import *
from Component import *
from Archive import *
from File import *
from Constraint import *

class XmlDataLoader(Doc4):
    
    def __init__(self):
        Doc4.__init__(self)
        self.logpath = self.config.get("extraction", "logfile")
        self.extractiondir = self.config.get("extraction", "outputdir")
        self.repositorydir = self.config.get("extraction", "mirror")
        self.extractorhome = self.config.get("extraction", "scriptdir")
        self.extractionskip = eval(self.config.get("extraction", "skipifextracted"))
        
        
    def load_metadata(self, id_snapshot, metadata_file):
        
        Session = sessionmaker(bind=self.engine)
        session = Session()
        
        data = open(metadata_file).read()
        entries = ET.fromstring(data)
        for archive_xml in entries:
            
            name = archive_xml.findtext("./name")
            home = archive_xml.findtext("./homepage")
            description = u'%s' % archive_xml.findtext("./description").replace("'", "\\'").replace("\"", "\\\"")
            group = archive_xml.findtext("./group")
            top_group = group
            idx = group.find("/") 
            if idx > 0:
                top_group = group[0:idx]
            packager_email = archive_xml.findtext("./email")
            summary = u'%s' % archive_xml.findtext("./summary")
            license = archive_xml.findtext("./license")
            
            component_stored = session.query(Component).filter_by(name=name).first()
            if component_stored is None:
                component = Component(name, summary, description, home, group, top_group, '', license)
                session.add(component)
                component_stored = session.query(Component).filter_by(name=name).first()
                id_component = component_stored.id_component
            else:
                id_component = component_stored.id_component
            
            
            
            filename = archive_xml.get("filename")
            version = archive_xml.findtext("./version")
            release = archive_xml.findtext("./release")

            archive = Archive(filename, name, version, release)
            archive.id_component = id_component
            archive.id_snapshot = id_snapshot
            archive.license = license
            
            archive.prein = u'%s' % archive_xml.findtext("./scripts/prein").replace("'", "\\'").replace("\"", "\\\"")
            archive.postin = u'%s' % archive_xml.findtext("./scripts/postin").replace("'", "\\'").replace("\"", "\\\"")
            archive.preun = u'%s' % archive_xml.findtext("./scripts/preun").replace("'", "\\'").replace("\"", "\\\"")
            archive.postun = u'%s' % archive_xml.findtext("./scripts/postun").replace("'", "\\'").replace("\"", "\\\"")
            #distrib = archive.findtext("./distribution")
            archive.repository_id = archive_xml.findtext("./repository")
            if archive.repository_id == None or archive.repository_id == "":
                archive.repository_id = 0
#            branch = archive_xml.findtext("./branch")
#            section = archive_xml.findtext("./section")
#            architecture = archive_xml.findtext("./arch")
            if filename is not None and filename.find(".src.rpm") > 0:
                archive.is_source = True
                architecture = "SRPMS"
#            if filename is not None and filename.find(".noarch.rpm") > 0:
#                architecture = "noarch"
                
            archive.from_archive = archive_xml.findtext("./sourcerpm")
            archive.build_host = archive_xml.findtext("./buildhost")
            archive.build_time = archive_xml.findtext("./buildtime")
            archive.size = archive_xml.findtext("./size")
            archive.creator = archive_xml.findtext("./packager")
            archive.changelog = u'%s' % archive_xml.findtext("./changelog").replace("\\", "\\\\").replace("\"", "\\\"").strip()
            archive.md5sum = compute_MD5(filename)
            #append("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"\",\"%s\",\"%s\",\"0\",NULL\n" % (pkgid, filename, version, release, architecture, repository_id, branch, section, packager_email, name, home, license, group, upper_group, summary, description, changelog, sourcepkg, packager, prein, postin, preun, postun, buildhost, buildtime, md5sum, size), packages_out)
            
            session.add(archive)
            
            archive_stored = session.query(Archive).filter_by(md5sum=archive.md5sum).first()
            id_archive = archive_stored.id_archive 
            
            for archive_file in archive_xml.findall("./file"): 
                path = archive_file.get("path")
                type = archive_file.get("type")
                size = archive_file.get("size")
                if archive_file.get("extra") is not None:
                    extra = archive_file.get("extra")
                else:
                    extra = 0 
                if archive_file.get("cat") is not None:
                    cat = archive_file.get("cat")
                else:
                    cat = "package"

                filename = os.path.basename(path)
                file = File(id_archive, id_snapshot, path, filename, type, cat, size, extra) 
                session.add(file)

            self.load_constraints(archive_xml, id_archive, id_snapshot, "provides", session)
            self.load_constraints(archive_xml, id_archive, id_snapshot, "requires", session)
            self.load_constraints(archive_xml, id_archive, id_snapshot, "conflicts", session)
            self.load_constraints(archive_xml, id_archive, id_snapshot, "suggests", session)
            self.load_constraints(archive_xml, id_archive, id_snapshot, "obsoletes", session)

    

        session.commit()
        session.close()

    def load_constraints(self, archive_xml, id_archive, id_snapshot, constraint_type, session):
        deps = archive_xml.findall("./" + constraint_type)
        for dep in deps:
            object = dep.get("name")
            version = ""
            if (constraint_type is "conflicts" or constraint_type is "suggests"):
                version = dep.get("version")
            constraint = Constraint(id_archive, id_snapshot, constraint_type, object, version)
            session.add(constraint)


def main():
    loader = XmlDataLoader()
    md= "320/junit-3.8.2-5.0.2mdv2009.1.src.rpm/metadata.xml"
    metadatafile = "/home/doc4/data/%s" % md
    loader.load_metadata(102, metadatafile)

if __name__ == "__main__":
    main()

                         